package com.ali.config;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Proxy;

/**
 * 动态代理工具类
 */
@Slf4j
public class DynamicProxyUtil {
    public static <T> T getProxy(Class<?> clazz, Object object) {
        //获取类加载器
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();

        //生成代理对象
        Object objectInstance = Proxy.newProxyInstance(classLoader, new Class<?>[]{clazz}, (proxy, method, args) -> {
            Object result = method.invoke(object, args);
            log.info("代理执行" + method.getName() + "方法");
            return result;
        });
        return (T) objectInstance;
    }
}